<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formAdminTrait{
    public function index_admin(Content $content)
    {
        return $content->title('管理端配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_admin()->render()}</div>
HTML
            );
    }
    protected function form_admin(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->html(function () {
            return '';
        }, '<b>一、管理页面菜单</b>');
        $form->html(function () {
            return '<a href="https://img.mini.minisns.cn/hbx/mobile_admin.jpg" target="_blank">点击查看，这里设置的是什么地方</a>';
        }, '');
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">如何进入移动端管理员？1、将账号设置为<a href="'.admin_url('settings/safe').'" target="_blank">超级管理员</a>，2、前端：我->设置->管理页面</div>';
        }, '');
        $form->table('app_admin_nav','格子菜单', function (NestedForm $table) {
            $table->text('name', '文本')->default('文字');
            $table->image('img', '图片')->help('<a href="https://img.mini.minisns.cn/images/nav/nav.png" download="app_share_vip_img.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->default('https://img.mini.minisns.cn/images/nav/nav.png')->uniqueName()->url('files/uploads')->autoUpload();
            $table->radio('type', '类型')->options(FieldUtils::getUrlTypes())->default(0);
            $table->text('target', '跳转id')->help('<a href="https://doc.minisns.cn/doc/44/" target="_blank">路径大全</a>');
        })->default(Settings::get('app_admin_nav', ''));

        $form->action('settings/action/admin');
        return $form;
    }

    public function admin(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_admin_nav', AdminUtils::_table_data($request->input('app_admin_nav')), true);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);

        return $this->response()->success('成功！');
    }

}
